package com.jnu.myaccount_drawer;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jnu.myaccount_drawer.ui.mainPage.MainPageFragment;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class InputActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView textViewInputType;
    private EditText editTextPrice;
    private int imageViewType;

    private ImageView imageViewFood;
    private ImageView imageViewRent;
    private ImageView imageViewSalary;
    private ImageView imageViewShopping;
    private ImageView imageViewFinancing;
    private ImageView imageViewTransportation;
    private ImageView imageViewGrocery;
    private ImageView imageViewMedical;
    private ImageView imageViewExercise;
    private ImageView imageViewOther;
    private Button buttonPayOrIncome;
    private Button buttonPayType;
    private TextView textViewDate;
    private String type;
    private int pictureId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_input);

        //获取当天日期并设置
        Calendar calendar = Calendar.getInstance();
        @SuppressLint("SimpleDateFormat")
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String stringDate = simpleDateFormat.format(calendar.getTime());
        TextView textViewDateNumber=findViewById(R.id.text_view_date_number);
        textViewDateNumber.setText(stringDate);

        Intent intent = getIntent();
        int position = intent.getIntExtra("position",0);

        //读MainActivity的数据
        textViewInputType = findViewById(R.id.text_view_input_type);
        editTextPrice = findViewById(R.id.edit_text_input_price);
        buttonPayOrIncome=findViewById(R.id.button_pay_or_income);
        buttonPayType=findViewById(R.id.button_pay_type);
        textViewDate=findViewById(R.id.text_view_date_number);

        //取数据
        type = intent.getStringExtra("type");
        double price = intent.getDoubleExtra("price",0);
        String payType=intent.getStringExtra("payType");
        String payOrIncome=intent.getStringExtra("payOrIncome");
        String date=intent.getStringExtra("date");
        pictureId = intent.getIntExtra("pictureId",0);
        int positionAll=intent.getIntExtra("positionAll",0);
        if(null!= type){//title为空，说明是添加数据，此处不为空是编辑数据
            price=Math.abs(price); //不论收入支出均显示正值
            textViewInputType.setText(type);//将原本的数据显示到编辑的页面中
            editTextPrice.setText(Double.toString(price));
            imageViewType=pictureId;
            buttonPayOrIncome.setText(payOrIncome);
            buttonPayType.setText(payType);
            textViewDate.setText(date);
            //不论收入支出均显示正值
        }

        Button buttonOK = this.findViewById(R.id.button_ok);
        buttonOK.setOnClickListener(view -> {
            Intent intent1 = new Intent();
            intent1.putExtra("position",position);
            intent1.putExtra("positionAll",positionAll);
            intent1.putExtra("type",textViewInputType.getText().toString());
            intent1.putExtra("price",Double.parseDouble(editTextPrice.getText().toString()));
            intent1.putExtra("pictureId",imageViewType);
            intent1.putExtra("payOrIncome",buttonPayOrIncome.getText().toString());
            intent1.putExtra("payType",buttonPayType.getText().toString());
            intent1.putExtra("date", textViewDate.getText().toString());
            setResult(MainPageFragment.RESULT_CODE_ADD_DATA, intent1);
            InputActivity.this.finish();
        });
        Button buttonCancel = this.findViewById(R.id.button_cancel);
        buttonCancel.setOnClickListener(view -> InputActivity.this.finish());
        //点击图片，更改账目类型文本，更改图片，将设置的账目类型传给mainActivity
        //一次只能打一个勾
        textViewInputType = this.findViewById(R.id.text_view_input_type);

        imageViewFood = this.findViewById(R.id.image_view_food);
        imageViewRent = this.findViewById(R.id.image_view_rent);
        imageViewSalary = this.findViewById(R.id.image_view_salary);
        imageViewShopping = this.findViewById(R.id.image_view_shopping);
        imageViewFinancing = this.findViewById(R.id.image_view_financing);
        imageViewTransportation = this.findViewById(R.id.image_view_transportation);
        imageViewGrocery = this.findViewById(R.id.image_view_grocery);
        imageViewMedical = this.findViewById(R.id.image_view_medical);
        imageViewExercise = this.findViewById(R.id.image_view_exercise);
        imageViewOther = this.findViewById(R.id.image_view_other);

        imageViewFood.setOnClickListener(this);
        imageViewRent.setOnClickListener(this);
        imageViewSalary.setOnClickListener(this);
        imageViewShopping.setOnClickListener(this);
        imageViewFinancing.setOnClickListener(this);
        imageViewTransportation.setOnClickListener(this);
        imageViewGrocery.setOnClickListener(this);
        imageViewMedical.setOnClickListener(this);
        imageViewExercise.setOnClickListener(this);
        imageViewOther.setOnClickListener(this);

        buttonPayOrIncome = this.findViewById(R.id.button_pay_or_income);
        buttonPayOrIncome.setOnClickListener(view -> {
            if(buttonPayOrIncome.getText().equals("支出")){
                buttonPayOrIncome.setText("收入");
            }
            else if(buttonPayOrIncome.getText().equals("收入")){
                buttonPayOrIncome.setText("支出");
            }
        });

        buttonPayType = this.findViewById(R.id.button_pay_type);
        buttonPayType.setOnClickListener(view -> {
            View dialoguePayType= LayoutInflater.from(InputActivity.this).inflate(R.layout.dialogue_pay_type,null);
            AlertDialog.Builder alertPayTypeDialogBuilder = new AlertDialog.Builder(InputActivity.this);
            alertPayTypeDialogBuilder.setView(dialoguePayType);

            TextView textViewCurrentType=dialoguePayType.findViewById(R.id.current_choose_type);
            Button buttonPayTypeCash = dialoguePayType.findViewById(R.id.button_pay_type_cash);
            buttonPayTypeCash.setOnClickListener(view1 -> {
                buttonPayType.setText("现金");
                textViewCurrentType.setText("现金");
            });
            Button buttonPayTypeWechat = dialoguePayType.findViewById(R.id.button_pay_type_wechat);
            buttonPayTypeWechat.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    buttonPayType.setText("微信");
                    textViewCurrentType.setText("微信");
                }
            });
            Button buttonPayTypeAlipay = dialoguePayType.findViewById(R.id.button_pay_type_alipay);
            buttonPayTypeAlipay.setOnClickListener(view12 -> {
                buttonPayType.setText("支付宝");
                textViewCurrentType.setText("支付宝");
            });
            alertPayTypeDialogBuilder.setPositiveButton("确定", (dialogInterface, i) -> {
            });
            alertPayTypeDialogBuilder.setCancelable(false).setNegativeButton ("取消", (dialogInterface, i) -> {
            });
            alertPayTypeDialogBuilder.create().show();
        });

        textViewDate =this.findViewById(R.id.text_view_date_number);
        CalendarView calendarView=this.findViewById(R.id.calender_view);
        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
                month=month+1;
                textViewDate.setText(new StringBuilder().append(year).append("-").append(month).append("-").append(dayOfMonth).toString());
            }
        });
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        switch(view.getId()) {
            case R.id.image_view_food:
                imageViewFood.setImageResource(R.drawable.food_selected);
                textViewInputType.setText(R.string.string_food);
                imageViewType=R.drawable.food;

                imageViewRent.setImageResource(R.drawable.rent);
                imageViewSalary.setImageResource(R.drawable.salary);
                imageViewShopping.setImageResource(R.drawable.shopping);
                imageViewFinancing.setImageResource(R.drawable.financing);
                imageViewTransportation.setImageResource(R.drawable.transportation);
                imageViewGrocery.setImageResource(R.drawable.grocery);
                imageViewMedical.setImageResource(R.drawable.medical);
                imageViewExercise.setImageResource(R.drawable.exercise);
                imageViewOther.setImageResource(R.drawable.other);
                break;
            case R.id.image_view_rent:
                imageViewRent.setImageResource(R.drawable.rent_selected);
                textViewInputType.setText(R.string.string_rent);
                imageViewType=R.drawable.rent;

                imageViewFood.setImageResource(R.drawable.food);
                imageViewSalary.setImageResource(R.drawable.salary);
                imageViewShopping.setImageResource(R.drawable.shopping);
                imageViewFinancing.setImageResource(R.drawable.financing);
                imageViewTransportation.setImageResource(R.drawable.transportation);
                imageViewGrocery.setImageResource(R.drawable.grocery);
                imageViewMedical.setImageResource(R.drawable.medical);
                imageViewExercise.setImageResource(R.drawable.exercise);
                imageViewOther.setImageResource(R.drawable.other);
                break;
            case R.id.image_view_salary:
                imageViewSalary.setImageResource(R.drawable.salary_selected);
                textViewInputType.setText(R.string.string_salary);
                imageViewType=R.drawable.salary;

                imageViewFood.setImageResource(R.drawable.food);
                imageViewRent.setImageResource(R.drawable.rent);
                imageViewShopping.setImageResource(R.drawable.shopping);
                imageViewFinancing.setImageResource(R.drawable.financing);
                imageViewTransportation.setImageResource(R.drawable.transportation);
                imageViewGrocery.setImageResource(R.drawable.grocery);
                imageViewMedical.setImageResource(R.drawable.medical);
                imageViewExercise.setImageResource(R.drawable.exercise);
                imageViewOther.setImageResource(R.drawable.other);
                break;
            case R.id.image_view_shopping:
                imageViewShopping.setImageResource(R.drawable.shopping_selected);
                textViewInputType.setText(R.string.string_shopping);
                imageViewType=R.drawable.shopping;

                imageViewFood.setImageResource(R.drawable.food);
                imageViewRent.setImageResource(R.drawable.rent);
                imageViewSalary.setImageResource(R.drawable.salary);
                imageViewFinancing.setImageResource(R.drawable.financing);
                imageViewTransportation.setImageResource(R.drawable.transportation);
                imageViewGrocery.setImageResource(R.drawable.grocery);
                imageViewMedical.setImageResource(R.drawable.medical);
                imageViewExercise.setImageResource(R.drawable.exercise);
                imageViewOther.setImageResource(R.drawable.other);
                break;
            case R.id.image_view_financing:
                imageViewFinancing.setImageResource(R.drawable.financing_selected);
                textViewInputType.setText(R.string.string_financing);
                imageViewType=R.drawable.financing;

                imageViewFood.setImageResource(R.drawable.food);
                imageViewRent.setImageResource(R.drawable.rent);
                imageViewSalary.setImageResource(R.drawable.salary);
                imageViewShopping.setImageResource(R.drawable.shopping);
                imageViewTransportation.setImageResource(R.drawable.transportation);
                imageViewGrocery.setImageResource(R.drawable.grocery);
                imageViewMedical.setImageResource(R.drawable.medical);
                imageViewExercise.setImageResource(R.drawable.exercise);
                imageViewOther.setImageResource(R.drawable.other);
                break;
            case R.id.image_view_transportation:
                imageViewTransportation.setImageResource(R.drawable.transportation_selected);
                textViewInputType.setText(R.string.string_transportation);
                imageViewType=R.drawable.transportation;

                imageViewFood.setImageResource(R.drawable.food);
                imageViewRent.setImageResource(R.drawable.rent);
                imageViewSalary.setImageResource(R.drawable.salary);
                imageViewShopping.setImageResource(R.drawable.shopping);
                imageViewFinancing.setImageResource(R.drawable.financing);
                imageViewGrocery.setImageResource(R.drawable.grocery);
                imageViewMedical.setImageResource(R.drawable.medical);
                imageViewExercise.setImageResource(R.drawable.exercise);
                imageViewOther.setImageResource(R.drawable.other);
                break;
            case R.id.image_view_grocery:
                imageViewGrocery.setImageResource(R.drawable.grocery_selected);
                textViewInputType.setText(R.string.string_grocery);
                imageViewType=R.drawable.grocery;

                imageViewFood.setImageResource(R.drawable.food);
                imageViewRent.setImageResource(R.drawable.rent);
                imageViewSalary.setImageResource(R.drawable.salary);
                imageViewShopping.setImageResource(R.drawable.shopping);
                imageViewFinancing.setImageResource(R.drawable.financing);
                imageViewTransportation.setImageResource(R.drawable.transportation);
                imageViewMedical.setImageResource(R.drawable.medical);
                imageViewExercise.setImageResource(R.drawable.exercise);
                imageViewOther.setImageResource(R.drawable.other);
                break;
            case R.id.image_view_medical:
                imageViewMedical.setImageResource(R.drawable.medical_selected);
                textViewInputType.setText(R.string.string_medical);
                imageViewType=R.drawable.medical;

                imageViewFood.setImageResource(R.drawable.food);
                imageViewRent.setImageResource(R.drawable.rent);
                imageViewSalary.setImageResource(R.drawable.salary);
                imageViewShopping.setImageResource(R.drawable.shopping);
                imageViewFinancing.setImageResource(R.drawable.financing);
                imageViewTransportation.setImageResource(R.drawable.transportation);
                imageViewGrocery.setImageResource(R.drawable.grocery);
                imageViewExercise.setImageResource(R.drawable.exercise);
                imageViewOther.setImageResource(R.drawable.other);
                break;
            case R.id.image_view_exercise:
                imageViewExercise.setImageResource(R.drawable.exercise_selected);
                textViewInputType.setText(R.string.string_exercise);
                imageViewType=R.drawable.exercise;

                imageViewFood.setImageResource(R.drawable.food);
                imageViewRent.setImageResource(R.drawable.rent);
                imageViewSalary.setImageResource(R.drawable.salary);
                imageViewShopping.setImageResource(R.drawable.shopping);
                imageViewFinancing.setImageResource(R.drawable.financing);
                imageViewTransportation.setImageResource(R.drawable.transportation);
                imageViewGrocery.setImageResource(R.drawable.grocery);
                imageViewMedical.setImageResource(R.drawable.medical);
                imageViewOther.setImageResource(R.drawable.other);
                break;
            default:
                imageViewOther.setImageResource(R.drawable.other_selected);
                textViewInputType.setText(R.string.string_other);

                imageViewType=R.drawable.other;

                imageViewFood.setImageResource(R.drawable.food);
                imageViewRent.setImageResource(R.drawable.rent);
                imageViewSalary.setImageResource(R.drawable.salary);
                imageViewShopping.setImageResource(R.drawable.shopping);
                imageViewFinancing.setImageResource(R.drawable.financing);
                imageViewTransportation.setImageResource(R.drawable.transportation);
                imageViewGrocery.setImageResource(R.drawable.grocery);
                imageViewMedical.setImageResource(R.drawable.medical);
                imageViewExercise.setImageResource(R.drawable.exercise);
        }
    }
}